import qbsrv_helper
import os

g_pos_server = {
    'links': 'tcpip(host=192.168.1.233:46210;DoBroadcast=None)',
    'ServerName': 'qbpr5devok          store2',
    'uid': 'Sysadmin',
    'pwd': ''
}

last_logid = 0
def srv(curs):
	global last_logid
	cur,sur = curs

	cur.execute('select logid from loginfo where appid=3 and logid>? order by logid desc', (last_logid,))
	logids = [f_x[0] for f_x in cur.fetchall()]
	if not logids: return

	cur.execute('select count(*) from logmessage where logid=? and logtype=1', (logids[0],))
	if not cur.fetchall()[0][0]: return

	fnz = None
	for logid in logids:
		cur.execute('select first logtext from logmessage where logid=? and logtype=2 order by messageid asc', (logid,))
		rows = cur.fetchall()
		if not rows: continue
		p = rows[0][0].split('#')
		if len(p) < 3: continue
		p = p[2].split(':', 1)
		if len(p) < 2: continue
		if p[0][-15:] != 'Processing File': continue
		fnz = os.path.basename(p[1].strip())
		break

	last_logid = logids[0]
	if not fnz: return


def main():
	qbsrv_helper.run_srv(g_pos_server, srv)

if __name__ == '__main__':
    main()

	
